package linklist

import "fmt"

type Node struct {
	data interface{}
	next *Node
}

type SingleLinkList struct {
	header,tail *Node
	length uint32
}

func NewSingleLinkList() *SingleLinkList {
	n := &Node{}
	return &SingleLinkList{
		header: n,
		tail: n,
		length: 0,
	}
}
//AddHead 从头节点添加节点
func (s *SingleLinkList) AddHead(v interface{})  {
	newNode := &Node{data: v}
	defer func() {
		s.length++
	}()
	if s.length == 0 {
		s.header.next = newNode
		s.tail = newNode
	}else {
		next := s.header.next
		s.header.next =newNode
		newNode.next = next
	}
}

//AddTail 从尾节点添加节点
func (s *SingleLinkList) AddTail(v interface{})  {
	newNode := &Node{data: v}
	defer func() {
		s.length++
	}()
	if s.length == 0 {
		//s.header.next = newNode
		s.tail.next = newNode
		s.tail = newNode
	}else {
		next := s.header.next
		s.header.next =newNode
		newNode.next = next
	}
}

func (s *SingleLinkList) Scan()  {
	cur := s.header.next
	i := 0
	for cur.next!=nil {
		fmt.Printf("%vth value: %v\n",i,cur.data)
		cur = cur.next
		i++
	}
	fmt.Printf("%vth value: %v\n",i,cur.data)
}